package cn.iocoder.yudao.module.wine.controller.admin.commissionrate;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wine.controller.admin.commissionrate.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.commissionrate.CommissionRateDO;
import cn.iocoder.yudao.module.wine.service.commissionrate.CommissionRateService;

@Tag(name = "管理后台 - 佣金比例配置")
@RestController
@RequestMapping("/wine/commission-rate")
@Validated
public class CommissionRateController {

    @Resource
    private CommissionRateService commissionRateService;

    @PostMapping("/create")
    @Operation(summary = "创建佣金比例配置")
    @PreAuthorize("@ss.hasPermission('wine:commission-rate:create')")
    public CommonResult<Long> createCommissionRate(@Valid @RequestBody CommissionRateSaveReqVO createReqVO) {
        return success(commissionRateService.createCommissionRate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新佣金比例配置")
    @PreAuthorize("@ss.hasPermission('wine:commission-rate:update')")
    public CommonResult<Boolean> updateCommissionRate(@Valid @RequestBody CommissionRateSaveReqVO updateReqVO) {
        commissionRateService.updateCommissionRate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除佣金比例配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wine:commission-rate:delete')")
    public CommonResult<Boolean> deleteCommissionRate(@RequestParam("id") Long id) {
        commissionRateService.deleteCommissionRate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得佣金比例配置")
    @PreAuthorize("@ss.hasPermission('wine:commission-rate:query')")
    public CommonResult<CommissionRateRespVO> getCommissionRate() {
        CommissionRateDO commissionRate = commissionRateService.getCommissionRateInfo();
        return success(BeanUtils.toBean(commissionRate, CommissionRateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得佣金比例配置分页")
    @PreAuthorize("@ss.hasPermission('wine:commission-rate:query')")
    public CommonResult<PageResult<CommissionRateRespVO>> getCommissionRatePage(@Valid CommissionRatePageReqVO pageReqVO) {
        PageResult<CommissionRateDO> pageResult = commissionRateService.getCommissionRatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CommissionRateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出佣金比例配置 Excel")
    @PreAuthorize("@ss.hasPermission('wine:commission-rate:export')")
    @OperateLog(type = EXPORT)
    public void exportCommissionRateExcel(@Valid CommissionRatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CommissionRateDO> list = commissionRateService.getCommissionRatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "佣金比例配置.xls", "数据", CommissionRateRespVO.class,
                        BeanUtils.toBean(list, CommissionRateRespVO.class));
    }

}
